<template>
  <div class="app-declare-container">
    <nu-col :span="24" style="width:100%;height:100%">
      <nu-row class="row" :gutter="20" ref="formData">
        <nu-col :span="24" class="col">
          <span class="hint">考核数据重算</span><br />
        </nu-col>
        <nu-col :span="24">
          <div class="line"></div>
        </nu-col>
      </nu-row>
      <el-radio-group v-model="examineType" @input="examineTypeInput">
        <el-radio-button label="1">发电计划考核</el-radio-button>
        <el-radio-button label="2">母线电压考核</el-radio-button>
        <el-radio-button label="3">调峰考核</el-radio-button>
        <el-radio-button label="4">agc考核</el-radio-button>
        <el-radio-button label="5">全部考核</el-radio-button>
      </el-radio-group>
      <NuVerifyForm :model="formData" ref="formData" label-width="100px" class="submitForm" @submitForm="submitForm"
        :buttons="fyButtons">
        <NuformItem label="通道组名称" prop="channelGroupId" width="auto" :rules="[{ required: true, message: '通道组名称不能为空' }]"
          v-if="examineType != 5">
          <nu-select class="searchSelect" :filterable="true" :list="channelList" type="text" :valueName="'id'"
            :label="'groupName'" v-model="formData.channelGroupId" autocomplete="off" style="width: 100%;"
            placeholder="请选择">
          </nu-select>
        </NuformItem>
        <NuformItem label="机组" v-if="examineType != '2' && examineType != 5" prop="deviceId" width="auto"
          :rules="[{ required: true, message: '机组不能为空' }]">
          <nu-select class="searchSelect" :filterable="true" :list="nameList" type="text" v-model="formData.deviceId"
            autocomplete="off" style="width: calc(100% - 5px); margin-left: 2px">
          </nu-select>
        </NuformItem>
        <NuformItem label="母线" v-if="examineType == '2' && examineType != 5" prop="deviceId" width="auto"
          :rules="[{ required: true, message: '母线不能为空' }]">
          <nu-select class="searchSelect" :filterable="true" :list="busNameList" type="text" v-model="formData.deviceId"
            autocomplete="off" style="width: calc(100% - 5px); margin-left: 2px">
          </nu-select>
        </NuformItem>
        <NuformItem label="考核周期" v-if="examineType != 5" prop="dailyAssessment" width="70%" :rules="[
          { required: true, message: '考核周期不能为空' },
        ]">
          <el-radio-group v-model="formData.dailyAssessment" @input="dailyAssessmentInput">
            <el-radio label="1">日考核</el-radio>
            <el-radio label="2">月考核</el-radio>
          </el-radio-group>
        </NuformItem>
        <NuformItem label="考核日期" prop="date" width="70%" :rules="[{ required: true, message: '考核日期不能为空' }]">
          <el-date-picker v-model="formData.date" :type="dateType" value-format="yyyy-MM-dd" placeholder="选择日期">
          </el-date-picker>
        </NuformItem>
        <NuformItem label="" prop="date" width="70%">
          <nu-button type="primary" class="searchButton" @click="formSave">重算</nu-button>
        </NuformItem>
      </NuVerifyForm>
      <!-- <NuTableList :showFilter="false" :columns="columns" :dataSource="dataSource" :options="options"
        :operates="operates(this)" :pagination="pagination" @handleSizeChange="handleSizeChange"
        @handleChangePage="handleChangePage" @handleCurrentChange="handleCurrentChange" style="margin: 0 0 20px 0">
      </NuTableList> -->
    </nu-col>
  </div>
</template>
<script>
import nuCol from "@/components/base/col.vue";
import nutbTitle from "@/components/base/tableTitle.vue";
import NuVerifyForm from "@/components/verify-form";
import NuformItem from "@/components/verify-form/form-item.vue";
import nuInput from "@/components/base/input";
import nuSelect from "@/components/base/select";
import nuButton from "@/components/base/button";
import NuTableList from "@/components/tableList/TableFormList";
import nuRow from "@/components/base/row.vue";
import nuDialog from "@/components/base/dialog";
import nuDatepickerLocal from "@/components/DatepickerLocal/VueDatepickerLocal";
import { getDateTime } from "@/utils/ruoyi";
const operates = (that) => [
  {
    label: "修改",
    icon: "",
    width: "50px",
    isShow: (row) => {
      return true;
    },
    disabled: (row) => {
      return false;
    },
    method: (row) => {
      that.openEdit(row);
    },
  },
  {
    label: "删除",
    icon: "",
    width: "50px",
    isShow: (row) => {
      return true;
    },
    disabled: (row) => {
      return false;
    },
    method: (row) => {
      that.deleteRow(row);
    },
  },
];
export default {
  name: "dataRecalculation",
  components: {
    nuCol,
    nutbTitle,
    NuVerifyForm,
    NuformItem,
    nuInput,
    nuSelect,
    nuButton,
    NuTableList,
    nuRow,
    nuDialog,
    nuDatepickerLocal
  },
  data() {
    return {
      operates,
      currentRow: null,
      formData: {
        channelGroupId: '',
        deviceId: '',
        dailyAssessment: '1',
        date: ''
      },
      fyButtons: [],
      examineType: '1',
      dateType: 'date',
      busNameList: [],
      nameList: [],
      channelList: []
    };
  },
  mounted() {
    // this.getDeclareData();
    this.getBidUnitName();
    this.getBusManageTableData();
    this.getChannelCondition();
  },
  methods: {
    /**
     * 考核input事件
     */
    examineTypeInput(val) {
      if (val == 5) {
        this.dateType = 'date'
      } else {
        if (val == 2) {
          this.formData.deviceId = this.busNameList[0].value
        } else {
          this.nameList.forEach(item => {
            if (item.label.indexOf('#1机组') > -1) {
              this.formData.deviceId = item.value
            }
          })
        }
        this.dailyAssessmentInput(this.formData.dailyAssessment)
      }
      this.$nextTick(() => {
        this.$refs['formData'].clearValidate('form');
      })
    },
    /**
     * 考核周期
     * @param {*} value 
     */
    dailyAssessmentInput(val) {
      if (val == 1) {
        this.dateType = 'date'
      } else if (val == 2) {
        this.dateType = 'month'
      }
    },
    /**
     * 重算
     */
    rerunClick() {
      let postData = {
        ...this.formData
      }
      let examineTypeMap = {
        1: { date: "checkScheduleDay", month: "checkScheduleMonth" },
        2: { date: "checkBusVoltDay", month: "checkBusVoltMonth" },
        3: { date: "checkPeakDay", month: "checkPeakMonth" },
        4: { date: "checkAgcDay", month: "checkAgcMonth" },
        5: { date: "handCheckTask", month: "handCheckTask" },
      }
      let url = examineTypeMap[this.examineType][this.dateType]
      var reg = new RegExp("-", "g")
      if (this.dateType == 'date') {
        if(this.examineType != 5) {
          postData['day'] = getDateTime('day', this.formData.date).replace(reg, "");
        }else {
          postData['day'] = getDateTime('day', this.formData.date);
        }
      } else {
        postData['month'] = getDateTime('month', this.formData.date).replace(reg, "");
      }
      if (this.examineType == 5) {
        postData['runDay'] = postData.day
        delete postData.channelGroupId
        delete postData.deviceId
        delete postData.day
        delete postData.month
      }
      delete postData.date
      delete postData.dailyAssessment
      this.$httpGet(window.api[url], postData)
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: "success",
            });
          } else {
            this.$message({
              message: res.msg,
              type: "info",
            });
          }
        })
        .catch((err) => {
        });
    },
    /**
     * 获取表格数据
     */
    getDeclareData() {
      // let postData = {};
      // for (let k in postData.query) {
      //   if (
      //     postData.query[k] &&
      //     Object.prototype.toString.call(postData.query[k]) == "[object String]"
      //   ) {
      //     postData.query[k] = postData.query[k].toString().trim();
      //   }
      // }
      this.$httpGet(window.api.peakList, this.pagination)
        .then((res) => {
          if (res.code) {
            // console.log(res.data)
            this.dataSource = res.rows;
            this.pagination.total = res.total;
          } else {
            this.dataSource = [];
          }
        })
        .catch((err) => {
          this.dataSource = [];
        });
    },
    /**
     * 获取机组
     */
    getBidUnitName() {
      let postData = {
        phyunitName: "",
        pageSize: 9999,
        pageNum: 1,
      };
      for (let k in postData.query) {
        if (
          postData.query[k] &&
          Object.prototype.toString.call(postData.query[k]) == "[object String]"
        ) {
          postData.query[k] = postData.query[k].toString().trim();
        }
      }
      this.dataSource = [];
      this.$httpGet(window.api.getCrewManageTableData, postData)
        .then((res) => {
          if (res.code == 200 && res.rows) {
            res.rows.map((item) => {
              this.nameList.push({
                label: item.phyunitName,
                value: item.phyunitId,
              });
              if (item.phyunitName.indexOf('#1机组') > -1) {
                this.formData.deviceId = item.phyunitId
              }
            });
          } else {
          }
        })
        .catch((err) => { });
    },
    /**
    * 获取母线
    */
    getBusManageTableData() {
      let postData = {
        busName: "",
        pageSize: 9999,
        pageNum: 1,
      };
      for (let k in postData.query) {
        if (
          postData.query[k] &&
          Object.prototype.toString.call(postData.query[k]) == "[object String]"
        ) {
          postData.query[k] = postData.query[k].toString().trim();
        }
      }
      this.dataSource = [];
      this.$httpGet(window.api.getBusManageTableData, postData)
        .then((res) => {
          if (res.code == 200 && res.rows) {
            res.rows.map((item) => {
              this.busNameList.push({
                label: item.busName,
                value: item.busId,
              });
            });
          } else {
          }
        })
        .catch((err) => { });
    },
    /**
     * 获取通道组列表
     */
    getChannelCondition() {
      let postData = {
        pageNum: 1,
        pageSize: 99999,
        channelName: ""
      };
      for (let k in postData.query) {
        if (postData.query[k] && Object.prototype.toString.call(postData.query[k]) == '[object String]') {
          postData.query[k] = postData.query[k].toString().trim();
        }
      }
      this.$httpGet(window.api.getChannelGroupSet, postData).then(res => {
        if (res.code == 200 && res.rows) {
          this.channelList = res.rows
          res.rows.forEach(item => {
            if (item.groupName == '华北网调') {
              this.formData.channelGroupId = item.id
              return false
            }
            // if (item.groupName.indexOf('华北网调') > -1) {
            //   this.formData.channelGroupId = item.id
            // }
          });

        } else {
        }
      }).catch(err => {

      });
    },
    /**
     * form组件内置查询事件
     */
    searchForm(val) {
      if (val) {
        this.getDeclareData();
      }
    },
    /**
     * 重算
     */
    formSave() {
      this.$refs["formData"].submitForm("form");
    },

    /**
     * 修改 接口
     */
    peakEdit() {
      let postData = {
        ...this.dialogFormData,
      };
      if (postData.bidUnitId instanceof Array) {
        postData.bidUnitId = postData.bidUnitId.join()
      }
      postData.deepPeakStartDate = getDateTime('', this.dialogFormData.time[0])
      postData.deepPeakEndDate = getDateTime('', this.dialogFormData.time[1])
      if (postData.relatedDevId == postData.relatedDevName) {
        postData.relatedDevId == postData.oldRelatedDevId;
      }
      for (let k in postData.query) {
        if (
          postData.query[k] &&
          Object.prototype.toString.call(postData.query[k]) == "[object String]"
        ) {
          postData.query[k] = postData.query[k].toString().trim();
        }
      }
      this.$httpPut(window.api.peakEdit, postData)
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: "success",
            });
            this.diaClose();
            this.getDeclareData();
          } else {
          }
        })
        .catch((err) => { });
    },

    /**
     * 弹窗-关闭
     */
    diaClose() {
      this.dialogShow = false;
      this.resetForm("dialogFormData");
      this.dialogFormData.time = [getDateTime('hours') + ':00', getDateTime('hours') + ':00']
    },

    /**
     * 重置表单
     */
    resetForm(formName) {
      this.$refs[formName].resetForm('form');
      for (let k in this[formName]) {
        if (Object.prototype.toString.call(this[formName][k]) == "[object String]") {
          this[formName][k] = ''
        } else if (Object.prototype.toString.call(this[formName][k]) == "[object Array]") {
          this[formName][k] = []
        } else if (Object.prototype.toString.call(this[formName][k]) == "[object Number]") {
          this[formName][k] = ''
        }
      }
    },
    /**
     * 每页条数变化事件
     */
    handleSizeChange(val) {
      this.pagination.pageNum = 1;
      this.pagination.pageSize = val;
      this.getDeclareData();
    },
    /**
     * 改变分页触发事件
     */
    handleChangePage(val) {
      this.pagination.pageNum = val;
      this.getDeclareData();
    },
    /**
     * 单选
     */
    handleCurrentChange(val) {
      this.currentRow = val;
    },

    /**
     * dialogFormData组件内置提交事件
     */
    submitForm(val) {
      if (val) {
        this.rerunClick()
      }
    },

    peakAddRow() {
      let postData = {
        ...this.dialogFormData,
      };
      postData.bidUnitId = postData.bidUnitId.join()
      postData.deepPeakStartDate = getDateTime('', this.dialogFormData.time[0])
      postData.deepPeakEndDate = getDateTime('', this.dialogFormData.time[1])
      for (let k in postData.query) {
        if (
          postData.query[k] &&
          Object.prototype.toString.call(postData.query[k]) == "[object String]"
        ) {
          postData.query[k] = postData.query[k].toString().trim();
        }
      }


      this.$httpPost(window.api.peakAdd, postData)
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              message: "添加成功",
              type: "success",
            });
            this.diaClose();
            this.getDeclareData();
          } else {
            this.$message({
              message: "添加失败",
              type: "info",
            });
          }
        })
        .catch((err) => { });
    },
    /**
     * 表格-修改
     */
    openEdit(row) {
      this.dialogTitle = "修改";
      this.dialogShow = true;
      if (row.bidUnitId instanceof Array) {

      } else {
        row.bidUnitId = row.bidUnitId.split(",")
      }
      Object.assign(this.dialogFormData, row);
    },

    // 删除一行
    deleteRow(row) {
      this.$confirm("确定要删除吗？", "确认删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delDeepPeakCommandOutput(row);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    delDeepPeakCommandOutput(row) {
      this.$httpDel(window.api.peakRemove, row.id)
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getDeclareData();
          } else {
          }
        })
        .catch((err) => { });
    },
  },
};
</script>
<style scoped>
.app-declare-container {
  height: calc(100vh - 70px);
}

.hint {
  display: block;
  font-size: 14px;
  line-height: 28px;
  color: rgba(79, 84, 112, 1);
}

.dialogHeaderSearch {
  background-color: var(--model-bgc);
  padding: 10px;
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  justify-content: right;
}

.el-col {
  margin: 0;
}

.row {
  margin-left: 0 !important;
  margin-right: 0 !important;
}

.col {
  padding: 0px 10px 0px 10px;
}

.submitForm {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  width: 500px;
}

::v-deep .el-form-item {
  margin: 20px 0 0 0;
}
</style>
